﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.IO;
using System.Xml;

namespace Common

{
   public class XmlHelper
    {
        public static string SerializeXmlToString<T>(T obj)
        {
            if (obj == null)
            {
                return null;
            }

            XmlSerializer serializer = new XmlSerializer(typeof(T));
            MemoryStream stream = new MemoryStream();
            XmlTextWriter xtw = new XmlTextWriter(stream, Encoding.UTF8);
            xtw.Formatting = Formatting.Indented;

            try
            {
                serializer.Serialize(stream, obj);
            }
            catch
            {
                //return null; 
            }

            stream.Position = 0;
            string returnStr = string.Empty;
            using (StreamReader sr = new StreamReader(stream, Encoding.UTF8))
            {
                string line = "";
                while ((line = sr.ReadLine()) != null)
                {
                    returnStr += line;
                }
            }


            return returnStr;
        }

        public static void SerializeXmlToFile<T>(T obj, string xmlPath)
        {
            if (obj == null) return;
            XmlSerializer serializer = new XmlSerializer(typeof(T));
            XmlTextWriter xtw = new XmlTextWriter(xmlPath, Encoding.UTF8);
            xtw.Formatting = Formatting.Indented;


        }

        /// <summary>
        /// 将对象序列化为xml
        /// </summary>
        /// <param name="o"></param>
        /// <returns></returns>
        public static void SerializeXml<T>(T o, string xmlPath)
        {
            XmlSerializer xs = new XmlSerializer(typeof(T));
            FileStream fs = new FileStream(xmlPath, FileMode.Append);
            xs.Serialize(fs, o);
            fs.Close();
        }

        //public static void SerializeXml2<T>(T o,string xmlPath)
        // {
        //  XmlSerializer s = new XmlSerializer(typeof(T));
        //  //TextWriter w = new StreamWriter(xmlPath, true, Encoding.UTF8);
        //  XmlWriterSettings xws=new XmlWriterSettings();
        //  xws.OmitXmlDeclaration=true;
        //  xws.Indent = true;

        //  XmlWriter xw = XmlWriter.Create(xmlPath,xws);

        // //Create our own namespaces for the output
        //    XmlSerializerNamespaces ns = new XmlSerializerNamespaces ();

        //     //Add an empty namespace and empty value
        //     ns.Add ("", "");
        //  s.Serialize(xw, o,ns);
        //  //w.Close(); 

        //  xw.Close();


        // }


        public static T DeserializeXml<T>(string xmlPath)
        {
            string xmlString = GetXmlToString(xmlPath);
            using (MemoryStream stream = new MemoryStream())
            {
                using (StreamWriter sw = new StreamWriter(stream, Encoding.UTF8))
                {
                    sw.Write(xmlString);
                    sw.Flush();
                    stream.Seek(0, SeekOrigin.Begin);
                    XmlSerializer serializer = new XmlSerializer(typeof(T));
                    try
                    {
                        return ((T)serializer.Deserialize(stream));
                    }
                    catch { return default(T); }

                }
            }
        }

        public static string GetXmlToString(string xmlPath)
        {
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(xmlPath);
            return doc.OuterXml;

        }

        public static void WriteFile(string xmlPath)
        {

        }
    }
}
